jclass={
    initPage:function(){
        // 绑定dom对象
        this.obj = {
            searchText:$('#searchText'),
        };
        // 声明全局变量
        this.data = {
            datatable:[],
            tableParams: {
                page:1,
                size:10,
                informationProblem: ''
            },
            addcgParams: {
                informationProblem:'',
                informationAnswer: ''
            }
        };
        this.getTableList();
        // this.creatTable({data:{},total:0});
        this.init();
        this.bindEvent();
    },
    init() {
        let that = this

        // 重置
        $("#resetClick").click(() => {
            $("#input_informationProblem").val('')
            this.data.tableParams = {
                page:1,
                size:10,
                informationProblem: ''
            }
        })

        // 查询
        $("#searchClick").click(() => {
            this.getTableList()
        })
    },
    // rgb转16进制
    colorRGBtoHex(color) {
        var rgb = color.split(',');
        var r = parseInt(rgb[0].split('(')[1]);
        var g = parseInt(rgb[1]);
        var b = parseInt(rgb[2].split(')')[0]);
        var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
        return hex;
     },
    bindEvent() {
        // $("#add_btn_cg").click(() => {
        //     let inputarr = $("#cgquestionDialog").find("input")
        //     let that = this
        //     let ok = true
        //     inputarr.each(function(index,ele) {
        //         if (!$(ele).val()) {
        //             $(this).attr("placeholder","不能是空!")
        //             ok = false
        //         }
        //     })
        //     if (ok) {
        //         this.data.addcgParams.informationProblem = $("#dialog_cgqust").val()
        //         this.data.addcgParams.informationAnswer = $("#dialog_cgans").val()
        //         if($("#jdialog_title").text() === '编辑') {
        //             jrequest.put("/api/command-problem",this.data.addcgParams).then(res => {
        //                 closeDialog()
        //             })
        //         } else {
        //             this.data.addcgParams.id = ''
        //             jrequest.post("/api/command-problem",this.data.addcgParams).then(res => {
        //                 closeDialog()
        //             })
        //         }
        //         this.getTableList()
        //     }
            
        // })

        $("#add_btn_cg").click(() => {
            let dataform = $("#form_addcg").serializeArray()
            let a = true
            console.log(dataform)
            dataform.forEach(item => {
                if(!item.value) a=false
            })
            if(a) {
                this.data.addcgParams.informationProblem = $("#dialog_cgqust").val()
                this.data.addcgParams.informationAnswer = $("#dialog_cgans").val()
                if($("#jdialog_title").text() === '编辑') {
                    jrequest.put("/api/command-problem",this.data.addcgParams).then(res => {
                        JPop.alert({msg:'修改成功！',type:'success'})
                        closeDialog()
                        this.getTableList()
                    })
                } else {
                    this.data.addcgParams.id = ''
                    jrequest.post("/api/command-problem",this.data.addcgParams).then(res => {
                        JPop.alert({msg:'添加成功！',type:'success'})
                        closeDialog()
                        this.getTableList()
                    })
                }
                return false
            }
            
        })
    },
    getTableList() {
        this.data.tableParams.informationProblem = $("#input_informationProblem").val()
        jrequest.get("/api/command-problem",this.data.tableParams).then(res => {
            res.data.forEach((item,index) => {
                item.index = index + 1
            })
            this.data.datatable = res.data
            $("#jtable_cgang").bootstrapTable('destroy');
            this.creatTable(res)
            // $("#jtable_cgang").bootstrapTable('load',res.data);
        })
    },
    creatTable:function(res){
        var self=this;
        var columnsOne = [
            {
                title : '序号',
                align: "center",
                formatter: function (value, row, index) {
                    //获取每页显示的数量
                    var pageSize=self.data.tableParams.size;
                    //获取当前是第几页
                    var pageNumber=self.data.tableParams.page;
                    //返回序号，注意index是从0开始的，所以要加上1
                    return pageSize * (pageNumber - 1) + index + 1;
                }
            },
            {
              field: "informationProblem",
              title: "信息问题"
            },
            {
              field: "informationAnswer",
              title: "信息答案"
            },
            {
              field: "createTime",
              title: "创建时间"
            },
            {
                field: 'Button',
                title: '操作',
                width: "200",
                align: 'center',
                events: operateEvents,
            }
        ];
        self.tableEvent("jtable_cgang",res.data,columnsOne,true,res.total);
    },
    tableEvent:function(id,data,columns,paginationShow,total){
        data.forEach(item => {
            item.Button = this.operateFormatter()
        })
        let that = this
        $(`#${id}`).bootstrapTable({
            method: 'get',
            cache: false,
            height: 320,
            pagination: paginationShow,
            pageSize: that.data.tableParams.size,
            pageNumber:that.data.tableParams.page,
            striped: true,
            columns: columns,
            sidePagination: 'server', // client or server
            totalRows: total,
            data :data,
            onPageChange: function (size, number) {
                console.log(number)
                that.data.tableParams.page = number
                that.data.tableParams.size = size
                that.getTableList()
            },
            formatNoMatches: function(){
                return '暂无数据';
            }
        });
    },
    operateFormatter() {
        return [
            '<button type="button" id="table_edit">修改</button>',
            '<button type="button" id="table_del">删除</button>'
            
        ].join('');
    }
    
}

$(function(){
    jclass.initPage();

    // 表格操作按钮
    window.operateEvents = {
        'click #table_del': (e,value,row,index) => {
            // console.log(row)
            // Jdialog.confirm({ message: `确定删除${row.informationProblem}?` }).on(function (e) {
            //     if (e) {
            //         jrequest.delete(`/api/command-problem/${row.id}`).then(res => {
            //             jclass.getTableList()
            //         })
            //     }
            // });
            JPop.dialogDel({msg:'确定删除?',operate:true}).on(success => {
                if (success) {
                    jrequest.delete(`/api/command-problem/${row.id}`).then(res => {
                        jclass.getTableList()
                    })
                }
            })
            
        },
        'click #table_edit': (e,value,row,index) => {
            // console.log(row)
            openDialog("edit")
            $("#dialog_cgqust").val(row.informationProblem)
            $("#dialog_cgans").val(row.informationAnswer)
            jclass.data.addcgParams.id = row.id
        }
    }
});

function openDialog(type) {
    type = type || ''
    if (type === 'edit') {
        $("#jdialog_title").text("编辑")
    } else {
        $("#jdialog_title").text("添加")
    }
    $("#cgquestionDialog").show()

}
function closeDialog() {
    $("#dialog_cgqust").val('')
    $("#dialog_cgans").val('')
    $("#cgquestionDialog input").attr("placeholder","请输入")
    $("#cgquestionDialog").hide()
}

